package cate.game.pvp.guildwar.data;

import cate.game.GameBody;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@NoteClass("公会战一方数据")
public class GuildWarFightSide {

    @NoteField("buff等级")
    public int buffLevel;

    @NoteField("公会简要信息")
    public GWGuildProfile profile;

    @NoteField(value = "据点数据",detail = "由对方的数据构建而成")
    public Map<Integer, GuildWarPt> ptMap = new HashMap<>();

    public void initialize() {
        if (profile == null) {
            profile = new GWGuildProfile();
        }
        if (ptMap == null) {
            ptMap = new HashMap<>();
        }
        ptMap.values().forEach(GuildWarPt::initialize);
    }

    public GuildWarPt getOrBuildPt(int ptId) {
        GuildWarPt pt = ptMap.get(ptId);
        if (pt == null) {
            pt = new GuildWarPt();
            pt.initialize();
            pt.ptId = ptId;
            ptMap.put(ptId, pt);
        }
        return pt;
    }

    public EcResult<Void> addBuffLevel(GameBody game) {
        EcResult<Void> r = new EcResult();
        if (game.table.guildWar.buff.get(buffLevel + 1) != null) {
            buffLevel++;
            return r.success();
        } else {
            return r.fail("buff等级已满");
        }
    }

    public int totalStar() {
        return this.ptMap.values().stream().mapToInt(p -> p.star).sum();
    }

    //检测是否轮空
    public boolean isEmpty(){
        return StringUtils.isEmpty(profile.uid);
    }

    public void clear() {
        buffLevel = 0;
        profile.clear();
        ptMap.clear();
    }



}
